package org.example.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author : Nico
 * @create 2025/4/3 14:01
 */
@RestController
@RequestMapping("/redisson")
public class RedissonController {

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/testBloom")
    public String testBloom() {

        RBloomFilter<String> bloom = redissonClient.getBloomFilter("name");
        long l = 80000000L;

        if (!bloom.isExists()) {
            // 初始化布隆过滤器；  大小:100000，误判率:0.01
            bloom.tryInit(l, 0.001);
        }

        // 新增10万条数据
        Set<Object> hashSet = new HashSet<>();
        int i1 = (int) l;
        for (int i = 0; i < i1; i++) {
            bloom.add("name" + i);
//            hashSet.add("name" + i);
        }
        // 判断不存在于布隆过滤器中的元素
        List<String> notExistList = new ArrayList<>();
        for (int i = 0; i < i1; i++) {
            String str = "name" + i;
            boolean notExist = bloom.contains(str);
            if (notExist&& !hashSet.contains(str)) {
                notExistList.add(str);
            }
        }
        bloom.expire(3000L, TimeUnit.MILLISECONDS);
        if (CollectionUtils.isNotEmpty(notExistList) && notExistList.size() > 0) {
            System.out.println("误判次数:" + notExistList.size());
        }
        return "success";
    }
}
